package com.jzr.bedside.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.bedside.db1.model.TDevice;
import com.jzr.bedside.db1.service.BedCardService;
import com.jzr.bedside.db1.service.RabbitMqService;
import com.jzr.bedside.db1.service.TDeviceService;
import com.jzr.bedside.db1.vo.BedCardVo;
import com.jzr.bedside.db1.vo.BedPatientVo;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/bedCard")
@Api(tags = "床头卡接口")
public class BedCardController {
    @Autowired
    private BedCardService bedCardService;

    @Autowired
    private TDeviceService deviceService;


    @Autowired
    RabbitMqService rabbitMqService;

    @ApiOperation(value="根据设备号获取床头卡信息")
    @GetMapping("/getBedCardInfo")
    public Result<BedPatientVo> getBedCardInfo(String deviceNo) {
        if (U.isBlank(deviceNo)) {
            return Result.fail("DeviceNo不能为空");
        }
        BedCardVo bedCardVo = bedCardService.getBedCardInfo(deviceNo);
        return Result.success("成功", bedCardVo);
    }

    @ApiOperation(value = "绑定床位")
    @PostMapping("/bindToBed")
    public Result<Integer> bindToBed(@ApiParam("设备信息") @RequestBody TDevice record) {
        System.out.println(record);
        int count;
        if (record == null) {
            return Result.fail("数据不能为空");
        }
        if (U.isBlank(record.getDeviceNo())) {
            return Result.fail("设备号不能为空");
        }
        if (U.isBlank(record.getRegisterBed())) {
            return Result.fail("床ID不能为空");
        }
        Integer bedId = bedCardService.hasBedId(record.getRegisterBed());
        if (U.isBlank(bedId)) {
            return Result.fail("床ID不存在");
        }
        try {
            count = deviceService.bindToBed(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("绑定失败");
        }
        if (count <= 0) {
            return Result.fail("绑定失败,0条记录");
        }


        return Result.success("绑定成功", count);
    }

    @GetMapping("/getDevice")
    @ApiOperation(value="获取设备信息")
    public Result<TDevice> getDevice(@ApiParam("设备号") String deviceNo){
        QueryWrapper<TDevice> qw = new QueryWrapper<>();
        qw.eq(TDevice.DeviceNo_,deviceNo);
        return Result.success("成功",deviceService.getOne(qw));
    }
}
